package com.github.serverlicense;

import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public final class ServerLicense extends JavaPlugin {

    private static final File TEMP_FOLDER = new File(System.getProperty("java.io.tmpdir"), "ServerLicense");

    @Override
    public void onLoad() {
        File pluginFolder = getFile().getParentFile();
        if (!ServerLicense.TEMP_FOLDER.exists() && !ServerLicense.TEMP_FOLDER.mkdirs()) {
            getLogger().severe("Failed to create temp folder: " + ServerLicense.TEMP_FOLDER.getAbsolutePath());
            return;
        }
        File[] files = pluginFolder.listFiles(f -> f.isFile() && f.getName().endsWith(".jar"));
        if (files == null) return;
        PluginModifier modifier = new PluginModifier(this);
        modifier.setPluginName(this.getName());
        for (File file : files) {
            if (file.getName().equals(getFile().getName())) continue;
            try {
                File tempFile = new File(ServerLicense.TEMP_FOLDER, file.getName());
                modifier.modifyJarWithJavassist(file, tempFile);
            } catch (Exception e) {
                getLogger().severe("Failed to modify jar: " + file.getName());
                getLogger().severe(e.getMessage());
            }
        }
    }

}
